﻿Public Class frmPruebaSeccion

    Public modeForm As Integer
    Public frmParent As frmMPruebaSeccion

    Private Sub frmPruebaSeccion_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case Me.modeForm
            Case 1 'Nuevo
                frmParent.tsbNuevo.Enabled = True
            Case 2 'Consultar
                frmParent.tsbConsultar.Enabled = True
            Case 3 'Editar
                frmParent.tsbEditar.Enabled = True
        End Select
    End Sub

    Private Sub frmPruebaSeccion_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.btnCancelar.Text = "Cancelar"
        Me.btnGuardar.Visible = True

        Dim arrRow1 As Array
        Dim dstRecord1 As New DataSet
        dstRecord1.Tables.Add("Pregunta")
        mdlCRUD.loadRecord(dstRecord1, "Pregunta", "pregunta", "", "pregunta_id")
        Me.dgvPreguntas.RowCount = dstRecord1.Tables("Pregunta").Rows.Count

        For i = 0 To dstRecord1.Tables("Pregunta").Rows.Count - 1
            arrRow1 = dstRecord1.Tables("Pregunta").Rows(i).ItemArray.ToArray

            Me.dgvPreguntas.Rows(i).Cells(0).Value = arrRow1(0)
            Me.dgvPreguntas.Rows(i).Cells(1).Value = False
            Me.dgvPreguntas.Rows(i).Cells(2).Value = arrRow1(1)
        Next i
        Select Case Me.modeForm
            Case 1 'Nuevo
                Me.txtCodigo.Text = ""
                Me.cmbPruebaSeccion.Text = ""
                Me.txtNombre.Text = ""
                Me.txtOrden.Text = ""

            Case 2 'Consultar
                Me.txtCodigo.Text = Me.frmParent.arrConsulta(0)

                If Me.frmParent.arrConsulta(1) <> "" Then
                    Me.cmbPruebaSeccion.SelectedValue = Me.frmParent.arrConsulta(1)
                End If

                Me.txtNombre.Text = Me.frmParent.arrConsulta(2)
                Me.txtOrden.Text = Me.frmParent.arrConsulta(3)

                Me.btnCancelar.Text = "Continuar"
                Me.btnGuardar.Visible = False

            Case 3 'Editar
                Dim dstRecord As New DataSet
                Dim strId As String = Me.frmParent.dgvPruebaSeccion.Rows(Me.frmParent.dgvPruebaSeccion.CurrentRow.Index).Cells(0).Value.ToString

                dstRecord.Tables.Add("pruebaseccion")
                mdlCRUD.loadRecord(dstRecord, "pruebaseccion", "pruebaseccion", "pruebaseccion_id = '" & strId & "'", Nothing)

                Dim arrRow As Array
                arrRow = dstRecord.Tables("pruebaseccion").Rows(0).ItemArray.ToArray

                Me.lblEdCodigo.Text = arrRow(0).ToString
                Me.txtCodigo.Text = arrRow(0).ToString

                If arrRow(1).ToString <> "" Then
                    Me.cmbPruebaSeccion.SelectedValue = arrRow(1).ToString
                End If

                Me.txtNombre.Text = arrRow(2).ToString
                Me.txtOrden.Text = arrRow(3).ToString

                dstRecord1.Tables.Add("pruebaseccionpregunta")
                mdlCRUD.loadRecord(dstRecord1, "pruebaseccionpregunta", "pruebaseccionpregunta", "pruebaseccion_id= '" & strId & "'", Nothing)

                'Me.dgvPreguntas.RowCount = dstRecord1.Tables("pruebaseccionpregunta").Rows.Count

                For i = 0 To dstRecord1.Tables("pruebaseccionpregunta").Rows.Count - 1
                    arrRow1 = dstRecord1.Tables("pruebaseccionpregunta").Rows(i).ItemArray.ToArray
                    If (arrRow1(1) = Me.dgvPreguntas.Rows(i).Cells(0).Value) Then
                        Me.dgvPreguntas.Rows(i).Cells(1).Value = True
                        Me.dgvPreguntas.Rows(i).Cells(3).Selected = True
                    End If
                Next i
        End Select
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGuardar.Click
        Select Case Me.modeForm
            Case 1 'Nuevo
                Dim arrCampos() As String = {"pruebaseccion_id", "prueba_id", "pruebaseccion_nombre", "pruebaseccion_orden"}
                Dim arrValor() As String = {Trim(Me.txtCodigo.Text), Trim(Me.cmbPruebaSeccion.SelectedValue.ToString), Trim(Me.txtNombre.Text), Trim(Me.txtOrden.Text)}

                mdlCRUD.insRecord("pruebaseccion", arrCampos, arrValor)

                'Ingreso de categorias x pregunta
                Dim strCondicion As String = "pruebaseccion_id = " & IIf(Trim(Me.txtCodigo.Text) <> "", "'" & Me.txtCodigo.Text & "'", "null")
                mdlCRUD.delRecord("pruebaseccionpregunta", strCondicion)
                For Each row In Me.dgvPreguntas.Rows
                    If row.Cells("chxPregunta").Value Then
                        Dim arrCamposPregunta() As String = {"pruebaseccion_id", "pregunta_id", "pruebaseccionpregunta_columna"}
                        Dim arrValorPregunta() As String = {Trim(Me.txtCodigo.Text), Trim(row.Cells("txtCodigoPregunta").Value), Trim(row.Cells("cbxColumna").Value)}

                        mdlCRUD.insRecord("pruebaseccionpregunta", arrCamposPregunta, arrValorPregunta)
                    End If
                Next

            Case 2 'Consultar

            Case 3 'Editar
                Dim arrParametro() As String = {
                                                "pruebaseccion_id = " & IIf(Trim(Me.txtCodigo.Text) <> "", "'" & Me.txtCodigo.Text & "'", "null"),
                                                "prueba_id = " & IIf(Trim(Me.cmbPruebaSeccion.SelectedValue.ToString) <> "", "'" & Me.cmbPruebaSeccion.SelectedValue.ToString & "'", "null"),
                                                "pruebaseccion_nombre = " & IIf(Trim(Me.txtNombre.Text) <> "", "'" & Me.txtNombre.Text & "'", "null"),
                                                "pruebaseccion_orden = " & IIf(Trim(Me.txtOrden.Text) <> "", "'" & Me.txtOrden.Text & "'", "null")
                                               }

                Dim strCondicion As String = "pruebaseccion_id = " & IIf(Me.lblEdCodigo.Text <> "", "'" & Me.lblEdCodigo.Text & "'", "null")

                mdlCRUD.updRecord("pruebaseccion", arrParametro, strCondicion)

                'Ingreso de categorias x pregunta
                mdlCRUD.delRecord("pruebaseccionpregunta", strCondicion)
                For Each row In Me.dgvPreguntas.Rows
                    If row.Cells("chxPregunta").Value Then
                        Dim arrCamposPregunta() As String = {"pruebaseccion_id", "pregunta_id", "pruebaseccionpregunta_columna"}
                        Dim arrValorPregunta() As String = {Trim(Me.txtCodigo.Text), Trim(row.Cells("txtCodigoPregunta").Value), Trim(row.Cells("cbxColumna").Value)}

                        mdlCRUD.insRecord("pruebaseccionpregunta", arrCamposPregunta, arrValorPregunta)
                    End If
                Next
        End Select

        If (mdlCRUD.blnFlag = True) Then
            Me.frmParent.refreshList()
            Me.Close()
        End If

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancelar.Click
        Select Case Me.modeForm
            Case 1 'Nuevo

            Case 2 'Consultar
                Me.frmParent.arrConsulta(0) = Trim(Me.txtCodigo.Text)

                If Me.cmbPruebaSeccion.SelectedIndex <> -1 Then
                    Me.frmParent.arrConsulta(1) = Trim(Me.cmbPruebaSeccion.SelectedValue.ToString)
                End If

                Me.frmParent.arrConsulta(2) = Trim(Me.txtNombre.Text)
                Me.frmParent.arrConsulta(3) = Trim(Me.txtOrden.Text)
                Me.frmParent.refreshList()

            Case 3 'Editar

        End Select


        Me.Close()
    End Sub



End Class